package com.nondo.dean.platform.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.nondo.dean.common.tool.StringUtils;
import com.nondo.dean.platform.domain.Area;
import com.nondo.dean.platform.domain.AreaExample;
import com.nondo.dean.platform.mapper.AreaMapper;
import com.nondo.dean.platform.model.AreaBean;

/**
 * 区县管理业务类
 * 
 * @author zhaoljin
 * @date 2018年4月3日
 */
@Component
public class AreaService {

    @Autowired
    private AreaMapper areaMapper;

    /**
     * 插入区县
     * @param area
     * @return
     */
    @Transactional
    public int addArea(Area area) {
        return areaMapper.insertSelective(area);
    }

    /**
     * 修改区县
     * @param area
     * @return
     */
    @Transactional
    public int updateArea(Area area) {
        return areaMapper.updateByPrimaryKeySelective(area);
    }

    /**
     * 按照主键ID查询区县
     * @param id
     * @return
     */
    public Area selectArea(Integer id) {
        return areaMapper.selectByPrimaryKey(id);
    }

    /**
     * 区县分页查询列表
     * @param offset
     * @param limit
     * @param area
     * @return
     */
    public List<Area> getAreaList(int offset, int limit, Area area) {
        AreaExample example = new AreaExample();
        RowBounds rowBounds = new RowBounds(offset, limit);
        AreaExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, area);
        return areaMapper.selectByExampleWithRowbounds(example, rowBounds);
    }

    /**
     * 区县总数统计
     * @param area
     * @return
     */
    public Long count(Area area) {
        AreaExample example = new AreaExample();
        AreaExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, area);
        return areaMapper.countByExample(example);
    }

    /**
     * 组装查询条件
     * @param criteria
     * @param area
     */
    void setCriteria(AreaExample.Criteria criteria, Area area) {
        if (area != null) {
            if (area.getId() != null)
                criteria.andIdEqualTo(area.getId());
            if (StringUtils.isNoneBlank(area.getName()))
                criteria.andNameLike("%" + area.getName() + "%");
        }
        criteria.andDelFlagEqualTo(String.valueOf(0));
    }

   /**
    * 按照区县名称查询区县
    * @param name
    * @return
    */
    public Area selectAreaByName(String name) {
        AreaExample example = new AreaExample();
        AreaExample.Criteria criteria = example.createCriteria();
        criteria.andNameEqualTo(name);
        criteria.andDelFlagEqualTo(String.valueOf(0));
        List<Area> areaList = areaMapper.selectByExample(example);
        return CollectionUtils.isNotEmpty(areaList)?areaList.get(0):null;
    }
    
    /**
     * 根据城市代码获取区县列表
     * @param code 市代码
     * @return
     */
    public List<AreaBean> getAreaListByCityCode(String code) {
        List<AreaBean> reulstList = new ArrayList<AreaBean>();
        AreaExample example = new AreaExample();
        AreaExample.Criteria criteria = example.createCriteria();
        criteria.andCityCodeEqualTo(code);
        criteria.andDelFlagEqualTo(String.valueOf(0));
        List<Area> areaList = areaMapper.selectByExample(example);
        for (Area area : areaList) {
            AreaBean areaBean = new AreaBean();
            areaBean = AreaBean.convertToBean(area);
            reulstList.add(areaBean);
        }
        return reulstList;
    }
}
